package com.roosterrobotics.replicant;


import com.roosterrobotics.replicant.provider.ReplicantData;
import android.app.ListActivity;
import android.content.ContentProvider;
import android.content.ContentProviderClient;
import android.content.Intent;
import android.database.Cursor;
import android.net.Uri;
import android.os.Bundle;
import android.view.ContextMenu;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ContextMenu.ContextMenuInfo;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.AdapterView.AdapterContextMenuInfo;
import android.widget.AdapterView.OnItemClickListener;


public class CommunityDetail extends ListActivity {
	private TextView mCommunityName;
	private ListView mDevicesList;
	private Long mCommunityId;
	private Button mAddDevice;
	
	private ContentProviderClient client;
	private ContentProvider provider;
	
	private static final int DELETE_ID = Menu.FIRST;
	private static final int UPDATE_ID = Menu.FIRST + 1;
	

	/**
	 * 
	 */
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		
	    client = getContentResolver().acquireContentProviderClient(ReplicantData.AUTHORITY);
	    provider = client.getLocalContentProvider();
		setContentView(R.layout.community_detail);
		setTitle(R.string.community_detail);	
		mDevicesList = getListView();
		registerForContextMenu(mDevicesList);
		
		
		mCommunityName = (TextView) findViewById(R.id.comDetailName);

		Bundle extras = getIntent().getExtras();
		mCommunityId = null;
		
		if (extras != null) {
			String name = extras.getString(ReplicantData.Communities.COMMUNITY_NAME_COLUMN_NAME);
			mCommunityId = extras.getLong(ReplicantData.Communities.COMMUNITY_ID_COLUMN_NAME);
			mCommunityName.setText(name);
		}
		// mCommunitId Must be set before the fillData.
		fillData();
		
		
		mAddDevice = (Button) findViewById(R.id.addDevice);
		mAddDevice.setOnClickListener(new View.OnClickListener() {			
			public void onClick(View v) {
				Intent i = new Intent(CommunityDetail.this, DeviceUpdate.class);
				Bundle b = new Bundle();
				b.putLong(ReplicantData.Devices.DEVICE_COMMUNITY_COLUMN_NAME, mCommunityId);
				// If you are creating a new device it can't be locked.
				b.putBoolean(ReplicantData.Devices.DEVICE_LOCK_COLUMN_NAME, false);
				// It also can't be tested so it isn't a replicant yet.
				b.putBoolean(ReplicantData.Devices.DEVICE_REPLICANT_COLUMN_NAME, false);
				i.putExtras(b);
				startActivity(i);
			}
		});
		
		mDevicesList.setOnItemClickListener(new OnItemClickListener() {
        	public void onItemClick(AdapterView <?> parent, View view, int position, long id) {
        		Cursor c = provider.query(Uri.parse(ReplicantData.Devices.CONTENT_URI + "/" + String.valueOf(id)),
        				null, null, null, null);
        		if (c != null) {
        			c.moveToFirst();
        			Long devId = c.getLong(c.getColumnIndex(ReplicantData.Devices.DEVICE_ID_COLUMN_NAME));
        			Long comId = c.getLong(c.getColumnIndex(ReplicantData.Devices.DEVICE_COMMUNITY_COLUMN_NAME));
        			Bundle b = new Bundle();
        			b.putLong(ReplicantData.Devices.DEVICE_ID_COLUMN_NAME, devId);
        			b.putLong(ReplicantData.Devices.DEVICE_COMMUNITY_COLUMN_NAME, comId);      			
        			c.close();
        			Intent i = new Intent(CommunityDetail.this, DeviceDetail.class);
        			i.putExtras(b);
        			startActivity(i);
        		}
        	}
        });
	}
	
	private void fillData() {
		if (mCommunityId != null) {
			Cursor c = provider.query(Uri.parse(ReplicantData.Devices.CONTENT_URI + "/" +
								ReplicantData.Communities.COMMUNITY_NAME + "/" +
								String.valueOf(mCommunityId)), null, null, null, null);    	
		
			startManagingCursor(c);
			DeviceCursorAdapter devices = new DeviceCursorAdapter(this, c);
			mDevicesList.setAdapter(devices);
		}
    }



	@Override
	public void onCreateContextMenu(ContextMenu menu, View v, ContextMenuInfo info) {
		super.onCreateContextMenu(menu, v, info);
		menu.add(0, DELETE_ID, 0, R.string.menu_delete);
		menu.add(0, UPDATE_ID, 0, R.string.menu_update);
	}
	
	@Override
	public boolean onContextItemSelected(MenuItem item) {
		AdapterContextMenuInfo info = (AdapterContextMenuInfo) item.getMenuInfo();
		switch(item.getItemId()) {
		case DELETE_ID:
			provider.delete(Uri.parse(ReplicantData.Devices.CONTENT_URI + "/" + String.valueOf(info.id)),
			null, null);
			fillData();
			return true;
		case UPDATE_ID:
			Cursor c = provider.query(Uri.parse(ReplicantData.Devices.CONTENT_URI + "/" + String.valueOf(info.id)),
	    				null, null, null, null);
			if (c != null) {
				Intent i = new Intent(this, CommunityUpdate.class);
    			String name = c.getString(c.getColumnIndex(ReplicantData.Devices.DEVICE_NAME_COLUMN_NAME));
    			Long devId = c.getLong(c.getColumnIndex(ReplicantData.Devices.DEVICE_ID_COLUMN_NAME));
    			Long comId = c.getLong(c.getColumnIndex(ReplicantData.Devices.DEVICE_COMMUNITY_COLUMN_NAME));
    			
    			String address = c.getString(c.getColumnIndex(ReplicantData.Devices.DEVICE_ADDRESS_COLUMN_NAME));
    			Long port = c.getLong(c.getColumnIndex(ReplicantData.Devices.DEVICE_PORT_COLUMN_NAME));
    			boolean lock = c.getInt(c.getColumnIndex(ReplicantData.Devices.DEVICE_LOCK_COLUMN_NAME)) > 0;
    			boolean replicant = c.getInt(c.getColumnIndex(ReplicantData.Devices.DEVICE_REPLICANT_COLUMN_NAME)) > 0;
    			Bundle b = new Bundle();
    			b.putString(ReplicantData.Devices.DEVICE_NAME_COLUMN_NAME, name);
    			b.putLong(ReplicantData.Devices.DEVICE_ID_COLUMN_NAME, devId);
    			b.putLong(ReplicantData.Devices.DEVICE_COMMUNITY_COLUMN_NAME, comId);
    			b.putLong(ReplicantData.Devices.DEVICE_PORT_COLUMN_NAME, port);
    			b.putString(ReplicantData.Devices.DEVICE_ADDRESS_COLUMN_NAME, address);
    			b.putBoolean(ReplicantData.Devices.DEVICE_LOCK_COLUMN_NAME, lock);
    			b.putBoolean(ReplicantData.Devices.DEVICE_REPLICANT_COLUMN_NAME, replicant);
				i.putExtras(b);
				c.close();
				startActivity(i);
			}
			return true;
			
			
		}
			
		return super.onContextItemSelected(item);
	}
}
